package com.xrtn.service.impl;

import com.xrtn.bean.*;
import com.xrtn.mapper.*;
import com.xrtn.service.IRoleService;
import com.xrtn.utils.ObjUtils;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 2018/3/21.
 */
@Service
public class RoleServiceImpl implements IRoleService{

    @Autowired
    private SysRoleInfoMapper sysRoleInfoMapper;

    @Autowired
    private SysMenuFunctionRelMapper sysMenuFunctionRelMapper;

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Override
    public List<SysRoleInfo> selectSysRoleList() {
        return sysRoleInfoMapper.selectAll();
    }

    /**
     * 获取职位（角色）权限
     *
     * @param role
     * @return
     */
    @Override
    public Map<String, Object> selectRoleList(Integer role) {
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("roleList", sysRoleInfoMapper.selectRoleList(role));

        SysRoleInfo roleInfo = sysRoleInfoMapper.selectByPrimaryKey(role);

        retMap.put("roleInfo", roleInfo);
        return retMap;
    }

    /**
     * 保存权限
     *
     * @param roleList
     * @return
     */
    @Override
    public Integer saveRole(SysRoleInfo roleInfo,String roleList) {

        Integer count = 0;
        if(ObjUtils.isEmpty(roleInfo.getRoleId())){
            //新增角色
            roleInfo.setRoleStatus(1);
            count += sysRoleInfoMapper.insertSelective(roleInfo);

            if(ObjUtils.isNotEmpty(roleList)){

                JSONArray jsonArray = JSONArray.fromObject(roleList);

                if(jsonArray.size() > 0 ){
                    for (int i =0; i<jsonArray.size(); i++){
                        JSONObject json = jsonArray.getJSONObject(i);
                        Integer isCheck = json.optInt("ischeck");
                        if(isCheck == 1){
                            SysMenuFunctionRel function = new SysMenuFunctionRel();
                            function.setFunctionId(json.optInt("functionId"));
                            function.setFunctionName(json.optString("functionName"));
                            function.setMenuId(json.optInt("menuId"));
                            function.setMenuName(json.optString("menuName"));
                            function.setRoleId(roleInfo.getRoleId());
                            function.setDeleteBut(1);
                            function.setSelectBut(1);
                            function.setUpdateBut(1);
                            function.setInsertBut(1);
                            count += sysMenuFunctionRelMapper.insertSelective(function);
                        }
                    }
                }
            }

        }else{
            //修改角色

            SysRoleInfo role = sysRoleInfoMapper.selectByPrimaryKey(roleInfo.getRoleId());
            role.setCreateTime(new Date());
            role.setRoleDesc(roleInfo.getRoleDesc());
            role.setRoleName(roleInfo.getRoleName());
            role.setSubclinicIds(roleInfo.getSubclinicIds());
            count += sysRoleInfoMapper.updateByPrimaryKeySelective(role);
            count += sysMenuFunctionRelMapper.deleteByRoleId(role.getRoleId());

            if(ObjUtils.isNotEmpty(roleList)){

                JSONArray jsonArray = JSONArray.fromObject(roleList);

                if(jsonArray.size() > 0 ){
                    for (int i =0; i<jsonArray.size(); i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        Integer isCheck = jsonObject.optInt("ischeck");
                        if(isCheck == 1){
                            SysMenuFunctionRel functionRel = new SysMenuFunctionRel();
                            functionRel.setFunctionId(jsonObject.optInt("functionId"));
                            functionRel.setFunctionName(jsonObject.optString("functionName"));
                            functionRel.setMenuId(jsonObject.optInt("menuId"));
                            functionRel.setMenuName(jsonObject.optString("menuName"));
                            functionRel.setRoleId(role.getRoleId());
                            functionRel.setDeleteBut(1);
                            functionRel.setSelectBut(1);
                            functionRel.setUpdateBut(1);
                            functionRel.setInsertBut(1);
                            count += sysMenuFunctionRelMapper.insertSelective(functionRel);
                        }
                    }
                }
            }

        }

        return count;
    }

    /**
     * 删除职位
     *
     * @param roleId
     * @return
     */
    @Override
    public Integer deleteRole(Integer roleId) {

        Map<String,Object> params = new HashMap<>();
        params.put("userRole" , roleId);
        List<SysUserInfo> userList = sysUserInfoMapper.selectUserListByMap(params);

        if(userList.size() > 0){
            return 0;
        }

        return sysMenuFunctionRelMapper.deleteByRoleId(roleId);
    }
}
